from PIL import Image
import os
import os.path

# 通过旋转、缩放、平移、翻转等操作将数据量扩增为原始数据集的八倍
# 由于需要制作样本txt文件，需对扩增的样本重命名


data_input_path = r'F:\临时存放地\formal_data\HER2\thrid_generation\train\20X\g3'
data_output_path = r'F:\临时存放地\pretreatment\thrid_generation_augmentation\g3'

# 加上原来自己的图片，数据增强了七次，即数据量为原来的8倍
def dataenrich(data_input_path,data_output_path):
    for parent, dirnames, filenames in os.walk(data_input_path):
        for filename in filenames:
            currentPath = os.path.join(parent, filename)
            im = Image.open(currentPath)

            pre_filename = filename.split('.')[0]

            # 原图
            out0 = im
            newname0 = data_output_path + '\\' + pre_filename + ".jpg"
            out0.save(newname0)

            # 逆时针旋转90度
            out1 = im.rotate(90)
            # out1.show()
            newname1 = data_output_path + '\\' + pre_filename + '_rotate90' + ".jpg"
            out1.save(newname1)

            # 逆时针旋转180度
            out2 = im.rotate(180)
            # out2.show()
            newname2 = data_output_path + '\\' + pre_filename + '_rotate180' + ".jpg"
            out2.save(newname2)

            # 逆时针旋转270度
            out3 = im.rotate(270)
            # out3.show()
            newname3 = data_output_path + '\\' + pre_filename + '_rotate270' + ".jpg"
            out3.save(newname3)

            # 水平翻转
            out4 = im.transpose(Image.FLIP_LEFT_RIGHT)
            newname4 = data_output_path + '\\' + pre_filename + '_flip' + ".jpg"
            out4.save(newname4)

            # 水平翻转，病逆时针旋转90度
            out5 = im.transpose(Image.FLIP_LEFT_RIGHT)
            out5 = out5.rotate(90)
            newname5 = data_output_path + '\\' + pre_filename + '_flip_rotate90' + ".jpg"
            out5.save(newname5)

            # 水平翻转，病逆时针旋转180度
            out6 = im.transpose(Image.FLIP_LEFT_RIGHT)
            out6 = out6.rotate(180)
            newname6 = data_output_path + '\\' + pre_filename + '_flip_rotate180' + ".jpg"
            out6.save(newname6)

            # 水平翻转，病逆时针旋转270度
            out7 = im.transpose(Image.FLIP_LEFT_RIGHT)
            out7 = out7.rotate(270)
            newname7 = data_output_path + '\\' + pre_filename + '_flip_rotate270' + ".jpg"
            out7.save(newname7)



if __name__ == '__main__':
    dataenrich(data_input_path,data_output_path)

